from django.db import models

# Create your models here.
from django.contrib.auth import get_user_model
from core.models import CoreModel


UserModel = get_user_model()

class ExamCategory(CoreModel):
    name = models.CharField(max_length=128, help_text='考试分类名称')
    tag = models.CharField(max_length=32, unique=True, help_text='标签')

    class Meta:
        db_table = 'exam_category'
        verbose_name = "考试分类"
        verbose_name_plural = verbose_name
        ordering = ("id",)
        
    def __str__(self) -> str:
        return self.name
    
    
class ExamLevel(CoreModel):
    name = models.CharField(max_length=128, help_text='考试难度名称')
    tag = models.CharField(max_length=32, unique=True, help_text='标签')

    class Meta:
        db_table = 'exam_level'
        verbose_name = "考试难度"
        verbose_name_plural = verbose_name
        ordering = ("id",)


class Exam(CoreModel):
    name = models.CharField(max_length=128, verbose_name="考试名称",help_text='考试名称')
    avatar  = models.ImageField(upload_to="exam/", verbose_name="考试预览图", default="exam/default.jpg",null=True, blank=True, help_text="考试预览图")
    score = models.IntegerField(default=0, verbose_name='当前考试的总分数', help_text='当前考试的总分数')
    limit_time = models.IntegerField(default=60, help_text='考试时长')
    start_datetime = models.DateTimeField(help_text='考试有效期开始时间')
    end_datetime = models.DateTimeField(help_text='考试有效期结束时间')
    
    class Meta:
        db_table = 'exam'
        verbose_name = "考试管理"
        verbose_name_plural = verbose_name
        
    def __str__(self) -> str:
        return self.name
    
    
class ExamSubject(CoreModel):
    exam = models.ForeignKey(Exam, on_delete=models.CASCADE, help_text='考试')
    question = models.ForeignKey('question.Question', on_delete=models.CASCADE, help_text='题目')
    score = models.IntegerField(default=0, help_text='题目分数')
    class Meta:
        db_table = 'exam_subject'
        verbose_name = "考试科目"
        verbose_name_plural = verbose_name
    
    
class ExamRecord(models.Model):
    exam = models.ForeignKey(Exam, on_delete=models.DO_NOTHING, db_constraint=False, help_text='考试ID')
    joiner = models.ForeignKey(UserModel, on_delete=models.DO_NOTHING, db_constraint=False, help_text='参与者ID')
    score = models.SmallIntegerField(default=0, help_text='得分')
    time_cost = models.SmallIntegerField(default=0, help_text='耗时')
    category_score = models.JSONField(default=dict, help_text='分类得分')
    start_time = models.DateTimeField(auto_now_add=True, help_text='开始时间')
    questions = models.ManyToManyField('question.Question', through='ExamAnswer', help_text='题目')
    
    class Meta:
        db_table = 'exam_record'
        verbose_name = "考试记录"
        verbose_name_plural = verbose_name
        
        
class ExamAnswer(models.Model):
    exam_record = models.ForeignKey(ExamRecord, on_delete=models.CASCADE, help_text='考试ID')
    question = models.ForeignKey('question.Question', on_delete=models.DO_NOTHING, db_constraint=False, help_text='题目ID')
    answer_options = models.JSONField(help_text='答案选项')
    
    class Meta:
        db_table = 'exam_answer'
        verbose_name = "考试题目"
        verbose_name_plural = verbose_name